<template>
	<el-dialog title="创建文件" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" controls-position="right" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="文件名称" prop="fileName" maxlength="50">
				<el-input v-model="form.fileName" placeholder="文件名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="上传文件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :limit="1" :data="{module:'personalfile'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<el-form-item label="文件说明">
				<el-input type="textarea" :rows="2" maxlength="380" placeholder="文件说明" v-model="form.remark"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>
<script>
import AttachMenu from "@/views/module/attachMenu";

export default {
	components: {
		AttachMenu
	},
	emits: ['success', 'closed'],
	props:{
		folderId:String
	},
	data() {
		return {
			apiObj: this.$API.file.attach.uploadFile,
			fileList: [],
			mode: "add",
			form: {
				folderId:this.folderId,
				fileId:"",
				sortNo: 0,
				attachId: "",
				fileName: "",
				remark: ''
			},
			rules: {},
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.file.personalFile.insertPersonalFile.post(this.form);
					}else if (this.mode == "edit") {
						res = await this.$API.file.personalFile.updatePersonalFile.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		addNewFile(res, file)
		{
			this.form.attachId = res.data.attachId;
		},
		//表单注入数据
		setData(data) {
			this.form.fileId = data.fileId;
			this.form.folderId = data.parentId;
			if(data.fileName.indexOf(".")>0)
			{
				this.form.fileName = data.fileName.split(".")[0]
			}else{
				this.form.fileName = data.fileName;
			}
			console.log(this.form.fileName);
			this.form.attachId = data.attachId;
			this.form.remark = data.remark;
		}
	}
}
</script>

<style>
</style>
